home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.02 Feb 93 / Screen I⁄O Classes / duApp.c next >
Encoding:
C/C++ Source or Header  |  1992-07-12  |  7.7 KB  |  305 lines  |  [TEXT/KAHL]

  1. /*****
  2.  * duApp.c
  3.  *
  4.  *    Application methods for a typical application.
  5.  *
  6.  *  Copyright © 1990 Symantec Corporation.  All rights reserved.
  7.  *
  8.  *****/
  9.  
  10. #include <CFWDesktop.h>
  11. #include "BUtilities.h"
  12. #include "duApp.h"
  13. #include "duMainDoc.h"
  14.  
  15. extern    OSType    gSignature;
  16. extern    CDesktop    *gDesktop;
  17.  
  18. #define        kExtraMasters        4
  19. #define        kRainyDayFund        20480
  20. #define        kCriticalBalance    20480
  21. #define        kToolboxBalance        20480
  22.  
  23.  
  24. /***
  25.  * IduApp
  26.  *
  27.  *    Initialize the application. Your initialization method should
  28.  *    at least call the inherited method. If your application class
  29.  *    defines its own instance variables or global variables, this
  30.  *    is a good place to initialize them.
  31.  *
  32.  ***/
  33.  
  34. void duApp::IduApp(void)
  35.  
  36. {
  37.     CApplication::IApplication( kExtraMasters, kRainyDayFund, 
  38.                         kCriticalBalance, kToolboxBalance);
  39.     
  40.  
  41. /*  The parameters to IApplication are the number of times to call  
  42.     MoreMasters, the total number of bytes of heap space to reserve for                       
  43.     monitoring low memory situations, and the portion of the memory
  44.     reserve to set aside for critical operations and toolbox calls.
  45.     
  46.     Four (4) is a reasonable number of MoreMasters calls,                           
  47.     but you should determine a good number for your application                     
  48.     by observing the heap using Lightsbug,                                              
  49.     TMON, or Macsbug. Set this parameter to zero, give your                         
  50.     program a rigorous work-out, then look at the heap and count                        
  51.     how many master pointer blocks have been allocated. Master                      
  52.     pointer blocks are nonrelocatable and have a size of $100                           
  53.     (hex). You should call MoreMasters at least this many                               
  54.     times -- add a few extra just to be safe. The purpose of all                        
  55.     this preflighting is to prevent heap fragmentation. You                             
  56.     don't want the Memory Manager to call MoreMasters and                           
  57.     create a nonrelocatable block in the middle of your heap. By                        
  58.     calling MoreMasters at the very beginning of the program,                           
  59.     you ensure that these blocks are allocated in a group at the                        
  60.     bottom of the heap. 
  61.                                                                         
  62.     The memory reserve is a safeguard for handling low memory                   
  63.     conditions and is used by the GrowMemory method in                              
  64.     CApplication (check there for more comments). In general,                           
  65.     your program should never request a memory block greater                        
  66.     than this reserve size without explicitly checking in                               
  67.     advance whether there is enough free memory to satisfy the                      
  68.     the request.
  69.                                                                                     
  70.  */
  71.  
  72. }
  73.  
  74.  
  75.  
  76. /***
  77.  * SetUpFileParameters
  78.  *
  79.  *    In this routine, you specify the kinds of files your
  80.  *    application opens.
  81.  *
  82.  *
  83.  ***/
  84.  
  85. void duApp::SetUpFileParameters(void)
  86.  
  87. {
  88.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  89.  
  90.         /**
  91.          **    sfNumTypes is the number of file types
  92.          **    your application knows about.
  93.          **    sfFileTypes[] is an array of file types.
  94.          **    You can define up to 4 file types in
  95.          **    sfFileTypes[].
  96.          **
  97.          **/
  98.  
  99.     sfNumTypes = 1;
  100.     sfFileTypes[0] = 'TEXT';
  101.  
  102.         /**
  103.          **    Although it's not an instance variable,
  104.          **    this method is a good place to set the
  105.          **    gSignature global variable. Set this global
  106.          **    to your application's signature. You'll use it
  107.          **    to create a file (see CFile::CreateNew()).
  108.          **
  109.          **/
  110.  
  111.     gSignature = '????';
  112. }
  113.  
  114.  
  115. /***
  116.  * SetUpMenus 
  117.  *
  118.  * Set up menus which must be created at run time, such as a
  119.  * Font menu. You can eliminate this method if your application
  120.  * does not have any such menus.
  121.  *
  122. ***/
  123.  
  124.  void duApp::SetUpMenus()
  125.  {
  126.  
  127.   inherited::SetUpMenus();  /*  Superclass takes care of adding     
  128.                                 menus specified in a MBAR id = 1    
  129.                                 resource    
  130.                             */                          
  131.  
  132.         /* Add your code for creating run-time menus here */    
  133.  }
  134.  
  135.  
  136.  
  137. /***
  138.  * DoCommand
  139.  *
  140.  *    Your application will probably handle its own commands.
  141.  *    Remember, the command numbers from 1-1023 are reserved.
  142.  *  The file Commands.h contains all the predefined TCL
  143.  *  commands.
  144.  *
  145.  *    Be sure to call the default method, so you can get
  146.  *    the default behvior for standard commands.
  147.  *
  148.  ***/
  149. void duApp::DoCommand(long theCommand)
  150.  
  151. {
  152.     switch (theCommand) {
  153.     
  154.         /* Your commands go here */
  155.     
  156.         default:    inherited::DoCommand(theCommand);
  157.                     break;
  158.     }
  159. }
  160.  
  161.  
  162. /***
  163.  *
  164.  * UpdateMenus 
  165.  *
  166.  *   Perform menu management tasks
  167.  *
  168. ***/
  169.  
  170.  void duApp::UpdateMenus()
  171.  {
  172.   inherited::UpdateMenus();     /* Enable standard commands */      
  173.  
  174.     /* Enable the commands handled by your Application class */ 
  175.  }
  176.  
  177.  
  178. /***
  179.  * Exit
  180.  *
  181.  *    Chances are you won't need this method.
  182.  *    This is the last chance your application gets to clean up
  183.  *  things like temporary files before terminating.
  184.  *
  185.  ***/
  186.  
  187. void duApp::Exit()
  188.  
  189. {
  190.     /* your exit handler here */
  191. }
  192.  
  193.  
  194. /***
  195.  * CreateDocument
  196.  *
  197.  *    The user chose New from the File menu.
  198.  *    In this method, you need to create a document and send it
  199.  *    a NewFile() message.
  200.  *
  201.  ***/
  202.  
  203. void duApp::CreateDocument()
  204.  
  205. {
  206. }
  207.  
  208. /***
  209.  * OpenDocument
  210.  *
  211.  *    The user chose Open… from the File menu.
  212.  *    In this method you need to create a document
  213.  *    and send it an OpenFile() message.
  214.  *
  215.  *    The macSFReply is a good SFReply record that contains
  216.  *    the name and vRefNum of the file the user chose to
  217.  *    open.
  218.  *
  219.  ***/
  220.  
  221. void duApp::OpenDocument(SFReply *macSFReply)
  222.  
  223. {
  224.     duMainDoc    *theDocument = NULL;
  225.     
  226.     TRY
  227.     {
  228.     
  229.         theDocument = new(duMainDoc);
  230.             
  231.             /**
  232.              **    Send your document an initialization
  233.              **    message. The first argument is the
  234.              **    supervisor (the application). The second
  235.              **    argument is TRUE if the document is printable.
  236.              **
  237.              **/
  238.         
  239.         theDocument->IduMainDoc(this, TRUE);
  240.     
  241.             /**
  242.              **    Send the document an OpenFile() message.
  243.              **    The document will open a window, open
  244.              **    the file specified in the macSFReply record,
  245.              **    and display it in its window.
  246.              **
  247.              **/
  248.         theDocument->OpenFile(macSFReply);
  249.     }
  250.     
  251.     CATCH
  252.     {
  253.         /*
  254.          * This exception handler gets executed if a failure occurred 
  255.          * anywhere within the scope of the TRY block above. Since 
  256.          * this indicates that the document could not be opened, we
  257.          * send it a Dispose message. The exception will propagate up to
  258.          * CSwitchboard's exception handler, which handles displaying
  259.          * an error alert.
  260.          */
  261.          
  262.          if (theDocument) theDocument->Dispose();
  263.     }
  264.     ENDTRY;
  265. }
  266.  
  267. /******************************************************************
  268.  * Run()
  269.  *
  270.  * Entry method to get into the event manager.
  271.  *
  272.  ******************************************************************/
  273.  
  274. void duApp::Run(void)
  275. {
  276.     char    *returnPtr;
  277.     Handle    textHdl;
  278.     
  279.     textHdl = NewHandleClear(1L);
  280.     returnPtr = NewPtrClear(256L);
  281.     GetInfo((char*)"A prompt string", returnPtr);
  282.     PtoCstr(returnPtr);
  283.     *textHdl = returnPtr;
  284.     DumpData(textHdl);
  285.     DumpBitMap(&thePort->portBits);
  286.     inherited::Run();
  287. }
  288.  
  289. /******************************************************************************
  290.  * MakeDesktop
  291.  *
  292.  * Create the global Desktop object, which is the top level of
  293.  *   the visual hierarchy. Overrride this method to use a desktop
  294.  *   which supports floating windows or other extensions to the
  295.  *   standard desktop.
  296.  *
  297.  ******************************************************************************/
  298.  
  299. void    duApp::MakeDesktop(void)
  300. {
  301.     gDesktop = new(CFWDesktop);
  302.     ((CFWDesktop*)gDesktop)->IFWDesktop(this);
  303. }    
  304.  
  305.